package com.lcy.demo;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.*;

/**
 * Created by 娄存银 on 2017/4/8.
 */
public class WordDocUtils {

    public static void export(String templateName, Map<String,Object> map, String outName) throws IOException, TemplateException {
        export("/",templateName,map,outName);
    }

    public static void export(String basePackagePath, String templateName, Map<String,Object> map, String outName) throws IOException, TemplateException {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
        configuration.setDefaultEncoding("utf-8");
        configuration.setDateFormat("yyyy-MM-dd HH:mm:ss");
        configuration.setClassLoaderForTemplateLoading(WordDocUtils.class.getClassLoader(),basePackagePath);

        Template template =configuration.getTemplate(templateName);
        File file = new File(outName);
        if(!file.exists() && !file.createNewFile()){
            return;
        }

        Writer writer = new OutputStreamWriter(new FileOutputStream(file));
        template.process(map,writer);
        writer.close();
    }

    public static String base64Encoding(InputStream inputStream)  {
        if(inputStream == null){
            return null;
        }

        byte[] bytes = null;
        try {
            bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if(bytes == null || bytes.length == 0){
            return null;
        }

        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(bytes);
    }

    public static String base64Encoding(File file){
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return base64Encoding(inputStream);
    }

    public static void main(String[] args){
        InputStream inputStream = WordDocUtils.class.getClassLoader().getResourceAsStream("test1.png");
        String penguins = base64Encoding(inputStream);
        InputStream stream = WordDocUtils.class.getClassLoader().getResourceAsStream("test.png");
        String desert = base64Encoding(stream);

        List<PoliceLog> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            PoliceLog log = new PoliceLog();
            log.setId((long) i);
            log.setDate("04-0"+i);
            log.setPoliceTime("11:5"+i);
            log.setPoliceContent("教师新村发现可疑人员");
            log.setArriveTime("12:0"+i);
            log.setResponseContent("询问为社区人员随后回家");
            log.setPoliceImageContent(penguins);
            log.setResponseImageContent(desert);
            list.add(log);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("logs",list);

        try {
            export("template.xml",map,"D:\\demo.doc");
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        }
    }
}
